package com.campusrecruitment.controller;

import com.campusrecruitment.common.JsonUtils;
import com.campusrecruitment.common.R;
import com.campusrecruitment.pojo.employer;
import com.campusrecruitment.pojo.loginUser;
import com.campusrecruitment.pojo.recruitMessage;
import com.campusrecruitment.pojo.resume;
import com.campusrecruitment.service.recruitMessageService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 招聘信息管理
 */
@Slf4j
@RestController
@RequestMapping("/recruitMessage")
public class recruitMessageController {

    private ObjectMapper mapper = new ObjectMapper();

    /**
     * 查询全部
     * @return
     */
    @PostMapping("/list")
    public R<List<recruitMessage>> queryRecruit(@RequestBody employer employer) throws JsonProcessingException {
        String name= employer.getName();

        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\recruitMessage.json"));
        String jsonList=jsonUtils.readJson(String.class);

        //读出所有招聘信息
        List<recruitMessage> recruitMessages= mapper.readValue(jsonList,new TypeReference<List<recruitMessage>>(){});
        //使用集合得到该单位的岗位数据
        List<recruitMessage> list=new ArrayList<>();
        for (recruitMessage r :recruitMessages
        ) {
            if(r.getEmployerName().equals(name)){
                list.add(r);
            }
        }
        return R.success(list);
    }

    /**
     * 修改
     * @param recruitMessage
     * @return
     */
    @PutMapping("/Update")
    public R<String>Update(@RequestBody recruitMessage recruitMessage) throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\recruitMessage.json"));
        String jsonList=jsonUtils.readJson(String.class);
        //读出所有招聘信息
        List<recruitMessage> recruitMessages= mapper.readValue(jsonList,new TypeReference<List<recruitMessage>>(){});

        for (int i=0;i<recruitMessages.size();i++
        ) {
            if (recruitMessages.get(i).getEmployerName().equals(recruitMessage.getEmployerName())
            &&recruitMessages.get(i).getName().equals(recruitMessage.getName())){
                //更新该用户
                recruitMessages.get(i).setId(recruitMessage.getId());
                recruitMessages.get(i).setName(recruitMessage.getName());
                recruitMessages.get(i).setAgeLimit(recruitMessage.getAgeLimit());
                recruitMessages.get(i).setAcademicRequirements(recruitMessage.getAcademicRequirements());
                recruitMessages.get(i).setTechnologyStack(recruitMessage.getTechnologyStack());
                recruitMessages.get(i).setSalary(recruitMessage.getSalary());
                recruitMessages.get(i).setSpareNumber(recruitMessage.getSpareNumber());
                break;
            }
        }

        String jsonString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(recruitMessages);
        jsonUtils.writeJson(jsonString);

        return R.success("修改成功");
    }

    /**
     * 查询
     * @param recruitMessage
     * @return
     */
    @PostMapping
    public R<List<recruitMessage>> queryRecruit(@RequestBody recruitMessage recruitMessage) throws JsonProcessingException {
        String employerName=recruitMessage.getEmployerName();
        String minSalary=recruitMessage.getMinSalary();
        String maxSalary=recruitMessage.getMaxSalary();
        String name=recruitMessage.getName();
        String id=recruitMessage.getId();

        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\recruitMessage.json"));
        String jsonList=jsonUtils.readJson(String.class);

        //所有招聘信息
        List<recruitMessage> recruitMessages= mapper.readValue(jsonList,new TypeReference<List<recruitMessage>>(){});

        List<recruitMessage> list=new ArrayList<>();

        if(id!="") {
            if (name != "") {
                if (minSalary != "") {   //三种条件存在
                    for (recruitMessage r : recruitMessages
                    ) {
                        if (r.getEmployerName().equals(employerName)
                                && r.getName().equals(name)
                                && r.getId().equals(id)
                                && (Integer.parseInt(r.getSalary()) < Integer.parseInt(maxSalary))
                                && (Integer.parseInt(r.getSalary()) > Integer.parseInt(minSalary))//转为int型判定大小
                        ) {
                            list.add(r);
                        }
                    }
                } else {  //名字，编号
                    for (recruitMessage r : recruitMessages
                    ) {
                        if (r.getEmployerName().equals(employerName)
                                && r.getName().equals(name)
                                && r.getId().equals(id)
                        ) {
                            list.add(r);
                        }
                    }
                }
            } else {  //id 薪资
                if (minSalary !=  "") {
                    for (recruitMessage r : recruitMessages
                    ) {
                        if (r.getEmployerName().equals(employerName)
                                && r.getId().equals(id)
                                && (Integer.parseInt(r.getSalary()) < Integer.parseInt(maxSalary))
                                && (Integer.parseInt(r.getSalary()) > Integer.parseInt(minSalary))//转为int型判定大小
                        ) {
                            list.add(r);
                        }
                    }
                }else {//只有id
                    for (recruitMessage r : recruitMessages
                    ) {
                        if (r.getEmployerName().equals(employerName)
                                && r.getId().equals(id)
                        ) {
                            list.add(r);
                        }
                    }
                }
            }
        }else{
            if (name !=  "") {
                if (minSalary !=  "") {   //名字，薪资
                    for (recruitMessage r : recruitMessages
                    ) {
                        if (r.getEmployerName().equals(employerName)
                                && r.getName().equals(name)
                                && (Integer.parseInt(r.getSalary()) < Integer.parseInt(maxSalary))
                                && (Integer.parseInt(r.getSalary()) > Integer.parseInt(minSalary))//转为int型判定大小
                        ) {
                            list.add(r);
                        }
                    }
                } else {  //只有名字
                    for (recruitMessage r : recruitMessages
                    ) {
                        if (r.getEmployerName().equals(employerName)
                                && r.getName().equals(name)
                        ) {
                            list.add(r);
                        }
                    }
                }
            } else {  //只有薪资
                if (minSalary !=  "") {
                    for (recruitMessage r : recruitMessages
                    ) {
                        if (r.getEmployerName().equals(employerName)
                                && (Integer.parseInt(r.getSalary()) < Integer.parseInt(maxSalary))
                                && (Integer.parseInt(r.getSalary()) > Integer.parseInt(minSalary))//转为int型判定大小
                        ) {
                            list.add(r);
                        }
                    }
                }
            }
            }
        return R.success(list);
    }
    /**
     * 添加
     * @return
     */
    @PostMapping("/save")
    public R<recruitMessage>save(@RequestBody recruitMessage recruitMessage) throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\recruitMessage.json"));
        String jsonList=jsonUtils.readJson(String.class);
        //读出所有招聘信息
        List<recruitMessage> recruitMessages= mapper.readValue(jsonList,new TypeReference<List<recruitMessage>>(){});

       recruitMessages.add(recruitMessage);

        String jsonString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(recruitMessages);
        jsonUtils.writeJson(jsonString);

        return R.success(recruitMessage);
    }

    @DeleteMapping
    public R<String>delete(@RequestBody recruitMessage recruitMessage) throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\recruitMessage.json"));
        String jsonList=jsonUtils.readJson(String.class);
        //读出所有招聘信息
        List<recruitMessage> recruitMessages= mapper.readValue(jsonList,new TypeReference<List<recruitMessage>>(){});
        for (int i=0;i<recruitMessages.size();i++
        ) {
            if (recruitMessages.get(i).getEmployerName().equals(recruitMessage.getEmployerName())
                    && recruitMessages.get(i).getId().equals(recruitMessage.getId())) {
                //删除该用户
                recruitMessages.remove(recruitMessages.get(i));
                break;
            }
        }
        String jsonString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(recruitMessages);
        jsonUtils.writeJson(jsonString);

        return R.success("删除成功！");
    }

    /**
     * 查询对应单位的岗位招聘信息
     * @return
     */
    @PostMapping("/getPosition")
    public R<List<recruitMessage>>getPosition(@RequestBody resume resume) throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\recruitMessage.json"));
        String jsonList=jsonUtils.readJson(String.class);
        //读出所有招聘信息
        List<recruitMessage> recruitMessages= mapper.readValue(jsonList,new TypeReference<List<recruitMessage>>(){});

        List<recruitMessage> list=new ArrayList<>();
        for (recruitMessage r:recruitMessages
             ) {
            if(r.getEmployerName().equals(resume.getEmployerName()))
                list.add(r);
        }
        return R.success(list);
    }


}
